//
//  ViewController.swift
//  原型模式
//
//  Created by 崔博 on 2017/7/12.
//  Copyright © 2017年 崔博. All rights reserved.
//

// 实现原型模式
import UIKit

struct Appointment {
    var name: String
    var day: String
    var place: String
    
    func printDetails(lable: String) {
        print("\(lable) with \(name) on \(day) at \(place)")
    }
}
class AppointmentClass: NSObject, NSCopying {
    var name: String
    var day: String
    var place: Location
    init(name: String, day: String, place: Location) {
        self.name = name
        self.day = day
        self.place = place
    }
    func printDetails(lable: String) {
        print("\(lable) with \(name) on \(day) at \(place.name) \(place.address)")
    }
    func copy(with zone: NSZone? = nil) -> Any {
        return AppointmentClass(name: self.name, day: self.day, place: self.place)
    }
}
class Location:NSObject, NSCopying {
    var name = ""
    var address = ""
    init(name: String, address: String) {
        self.name = name
        self.address = address
    }
    func copy(with zone: NSZone? = nil) -> Any {
        return Location(name: self.name, address: self.address)
    }
}
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        // Location 没有实现深拷贝 两个对象的loaction 指向同一块空间
        let beerMeeting = AppointmentClass(name: "Bob", day: "Mon", place: Location(name: "123", address: "123"))
        let workMeeing = beerMeeting.copy() as! AppointmentClass
        workMeeing.name = "Alice"
        workMeeing.place.name = "Confre RM2"
        workMeeing.place.address = "Company HQ"
        
        beerMeeting.printDetails(lable: "SUM")
        workMeeing.printDetails(lable: "Cuibo")
        structExplame()
        
}
    // 使用结构体来克隆对象  打印结果不一样
    func structExplame() {
        let beerMeeting = Appointment(name: "Bob", day: "Mon", place: "Bar")
        
        var workMetting = beerMeeting
        workMetting.name = "Alice"
        workMetting.day = "fir"
        workMetting.place = "china"
        
//        beerMeeting.printDetails(lable: "cuibo")
//        workMetting.printDetails(lable: "wangwenjie")
        
    }
    // 对象中不嵌套对象的深拷贝
//    func classExmple() {
//        
//        let beerMeeting = AppointmentClass(name: "Bob", day: "Mon", place: "Bar")
//        
//        //         深拷贝 拷贝对象
//        var workMetting = beerMeeting.copy() as! AppointmentClass
//        workMetting.name = "Alice"
//        workMetting.day = "fir"
//        workMetting.place = "china"
//        
//        beerMeeting.printDetails(lable: "cuibo")
//        workMetting.printDetails(lable: "wangwenjie")
//    }
}

